package com.myappengine.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.myappengine.model.Clazz;
import com.myappengine.model.Instructor;
import com.myappengine.model.Student;
import com.myappengine.model.UploadedFile;
import com.myappengine.service.ClassService;
import com.myappengine.service.StudentService;

@Controller
@RequestMapping("/students")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ClassService classService;
	
	@RequestMapping(value = "/add/{id}/{name}/{age}", method = RequestMethod.GET)
	public String addStudent(@PathVariable("id") String id,
			@PathVariable("name") String name,
			@PathVariable("age") int age,
			ModelMap model) {
		Student s = new Student(id, name, age);
		studentService.persistStudent(s);
		return "redirect:/students";
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String getStudents(ModelMap model) {
		model.addAttribute("students", studentService.getAllStudents());
		return "listStudents";
	}
	
	@RequestMapping(value = "/getClassesOfStudent/{studentKey}", method = RequestMethod.GET)
	public String getClassesOfStudent(@PathVariable("studentKey") String studentKey, Model model) {
		
		Clazz c = new Clazz("234234", "Computer network");
		studentService.addStudentToClass(studentKey, c);
		
		List<Clazz> classes = studentService.getClassesByStudent(studentKey);
		model.addAttribute("classes", classes);
		return "listClassesByStudent";
	}
	
	@RequestMapping(value = "/assignInstructor/{studentKey}", method = RequestMethod.GET)
	public String assignInstructor(@PathVariable("studentKey") String studentKey) {
		Instructor instructor = new Instructor("1", "Tho", "Ph.D");
		studentService.assignInstructorToStudent(studentKey, instructor);
		System.out.println("instructor");
		return "redirect:/students";
	}
	
	@RequestMapping(value = "/upload/{studentKey}", method = RequestMethod.GET)
	public String studentUploadFile(@PathVariable("studentKey") String studentKey) {
		UploadedFile file = new UploadedFile("File name", "image/png");
		studentService.addFileToStudent(studentKey, file);
		return "redirect:/students";
	}
	
	@RequestMapping(value = "/delete/{studentKey}", method = RequestMethod.GET)
	public String deleteStudent(@PathVariable("studentKey") String studentKey) {
		studentService.deleteStudent(studentKey);
		return "redirect:/students";
	}
	
}
